package com._51cto.healthy.controller;

import com._51cto.healthy.content.R;
import com._51cto.healthy.security.SecurityUser;
import com._51cto.healthy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpSession;


@Api(value = "管理用户认证管理", tags = "管理用户认证管理")
@CrossOrigin
@RestController
public class LoginController {

    @DubboReference
    private UserService userService;

    @ApiOperation(value = "用户名验证",notes="管理用户的登录名验证",tags = "管理用户认证管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "管理用户名")})
    @GetMapping("/valid-username")
    public R validUsername(
            @RequestParam("username") String username) {
        //方法一
        //User user = userService.validUsername(username);
        //if (user != null) {
        //return R.success("用户名存在");
        //} else {
        //    return R.fail("用户名不存在");
        //}

        //方法二
        userService.validUsername(username);
        return R.success("用户名存在");

        //方法三
        // userService.validUsername(username);
        // return R.success("用户名存在666");

    }
//    @ApiOperation(value = "用户登录",notes="用户提交的用户名与用户密码",tags = "管理用户认证管理")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "user", value = "用户名与用户密码")})
//    @PostMapping("/login")
//    public R login(@RequestBody LoginUser user, @ApiIgnore  HttpSession session){
//        //获取用户名和密码
//        User login = userService.login(user.getUsername(),user.getPassword());
//        //随机生成一串字符串,作为token的唯一标识
//        String token = UUID.randomUUID().toString();
//        //把token,login存入session中
//        session.setAttribute(token,login);
//        return R.success(token,"登陆成功");
//    }

    @ApiOperation(value = "用户信息获取",notes="使用token获取用户信息",tags = "管理用户认证管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "管理用户信息")})
    @GetMapping("/login-info")
    public R<SecurityUser> loginInfo(@RequestParam("token") String token , @ApiIgnore HttpSession session){
        //把存入session的token取出来
        Object obj = session.getAttribute(token);
        //断言判断obj类是否是User类,并返回信息
        Assert.isInstanceOf(SecurityUser.class,obj,"用户数据错误,请重新输入");
        return R.success((SecurityUser) obj,"用户信息获取成功");
    }

//    @ApiOperation(value = "登录用户退出", notes = "登录登出，清除Session", tags = "管理用户认证管理")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "token", value = "登录用户Token")})
//    @PostMapping("/logout")
//    public R logout(@RequestParam("token")String token ,@ApiIgnore HttpSession session){
//        //移除session中的token信息
//        session.removeAttribute(token);
//        return R.success();
//    }
}
